<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\File;
use think\Session;

class Index extends Controller
{
    public function hello(Request $request){
        if(Session::has('userid')){
            return Session::get('userid');
        }
        else{
            return json(null,500);
        }
    }
    public function getClass(){
        if(Session::has('userid')){
            $id = Session::get('userid');
            $list = Db::query('select * from classify where userid = ?',[$id['id']]);
            return $list;
        }
        else{
            return json(null,500);
        }
    }
    public function getClassByid(Request $request){
        $userid = $request->only('userid');
            $list = Db::query('select * from classify where userid = ?',[$userid['userid']]);
            $class=[];
            for($i = 0;$i<count($list);$i++){
                $album = Db::query('select * from album where classid=?',[$list[$i]['id']]);
                
                array_push($class,['classify'=>$list[$i],'album'=>$album]);
            }
            return $class;
    }
    public function getAlbum(Request $request){
        $userid = $request->only('id');
            $list = Db::query('select * from classify where id = ?',[$userid['id']]);
            $class=[];
            for($i = 0;$i<count($list);$i++){
                $album = Db::query('select * from album where classid=?',[$list[$i]['id']]);
                $user = Db::query('select id,name,icon from user where id=?',[$list[$i]['userid']]);
                array_push($class,['classify'=>$list[$i],'album'=>$album,'user'=>$user[0]]);
            }
            return $class[0];
    }
    public function getLamByUserid(Request $request){
        $userid = $request->only('userid');
        $list = Db::query('select lam.*,user.`name`,user.icon from lam left join user on user.id=lam.userid where userto = ? order by date desc',[$userid['userid']]);
        return $list; 
    }

    public function sendMsg(Request $request){
        $param = $request->post(false);
        $userid=$param['userid'];
        $userto = $param['userto'];
        $content = $param['content'];
        $c = Db::execute('insert into lam (content,userid,userto,date) values (?,?,?,now())',[$content,$userid,$userto]);
        return $c;
    }
    public function delMsg(Request $request){
        $param = $request->post(false);
        $id = $param['id'];
        $c = Db::execute('delete from lam where id=?',[$id]);
return $c;
    }
    public function addClass(Request $request){

        if(Session::has('userid')){
            $param = $request->post(false);
            $name = $param['name'];
            $id = Session::get('userid')['id'];
            $list = Db::query('select * from classify where userid=\''.$id.'\' and   name=\''.$name.'\'');
            if(count($list)>0){
                return json('已存在相册',500);
            }
            $a = Db::execute('insert into classify (name,userid,total) values(?,?,?)',[$name,$id,0]);
            if($a>0){
                $l =  Db::query('select * from classify where userid=\''.$id.'\' and   name=\''.$name.'\'');
                return $l[0];
            }else{
                return json('服务器错误',500);
            }
        }
        else{
            return json('未登录',500);
        }
    }
    

    public function like(Request $request){
        if(!Session::has('userid')){
            return '';
        }
        $param = $request->post(false);
        $id=$param['id'];
        $list = Db::query('select 1 from `like` where albumid=\''.$id.'\' and userid=\''.Session::get('userid')['id'].'\'');
        if(count($list)>0){
            Db::execute('delete from `like` where albumid=\''.$id.'\' and userid=\''.Session::get('userid')['id'].'\'');
        }else{
            Db::execute('insert into `like` (albumid,userid) values (?,?)',[$id,Session::get('userid')['id']]);
        }
    }  
    public function star(Request $request){
        if(!Session::has('userid')){
            return '';
        }
        $param = $request->post(false);
        $id=$param['id'];
        $list = Db::query('select 1 from star where albumid=\''.$id.'\' and userid=\''.Session::get('userid')['id'].'\'');
        if(count($list)>0){
            Db::execute('delete from star where albumid=\''.$id.'\' and userid=\''.Session::get('userid')['id'].'\'');
        }else{
            Db::execute('insert into star (albumid,userid) values (?,?)',[$id,Session::get('userid')['id']]);
        }
    }   
    public function isstar(Request $request){
        $param = $request->post(false);
        $id=$param['id'];
        $list = Db::query('select 1 from star where albumid=\''.$id.'\' and userid=\''.Session::get('userid')['id'].'\'');
        $list1 = Db::query('select 1 from `like` where albumid=\''.$id.'\' and userid=\''.Session::get('userid')['id'].'\'');
        $star = Db::query('select 1 from star where albumid=\''.$id.'\'');
        $like = Db::query('select 1 from `like` where albumid=\''.$id.'\'');
        return json(['islike'=>count($list1)>0,'isstar'=>count($list)>0,'star'=>count($star),'like'=>count($like)],200);
    }   
    public function Login(Request $request){
        $param = $request->post(false);
        $mail = $param['mail'];
        $psd = $param['psd'];
        $list = Db::query('select id,icon,name from user where mail=\''.$mail.'\''.' and psd=\''.$psd.'\'');
        if(count($list)>0){
            if(Session::has('userid')){
                return $list[0];
            }
            Session::set('userid',$list[0]);
            return json($list[0]);
        }
        return json('请检查邮箱或密码是否正确！',500);

    }
    public function getuser(Request $request){
        $param = $request->param();
        $id = $param['id'];
        $user = Db::query('select icon,name from user where id = ?',[$id]);
        if(count($user)>0){
            return $user[0];
        }
    }
    public function Register(Request $request){
        $param = $request->post(false);
        $mail = $param['mail'];
        $name = $param['name'];
        $psd = $param['psd'];
        $file = request()->file('image');
        $icon='';
        $list = Db::query('select 1 from user where mail=\''.$mail.'\'');
        if(count($list)>0){
            return json('本邮箱已注册',500);
        }
        $list = Db::query('select 1 from user where name=\''.$name.'\'');
        if(count($list)>0){
            return json('昵称已存在',500);
        }
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $icon = $info->getSaveName();
                $id=md5(time() . mt_rand(1,1000000));
                $r = Db::execute('INSERT INTO `user` (id,name,psd,icon,mail) VALUES (?,?,?,?,?)',[$id,$name,$psd,$icon,$mail]);
                if($r==1){
                    if(!Session::has('userid')){
                        Session::set('userid',['id'=>$id,'name'=>$name,'mail'=>$mail,'icon'=>$icon]);
                    }
                    return json('注册成功',200);
                }else{
                    return json("注册失败",500);
                }
            }else{
                return json("注册失败",500);
            }
        }else{
            return json("注册失败",500);
        }
    }
    public function searchAlbumList(Request $request){
        $param = $request->param();
        $text = $param['text'];
        $list = Db::query('select album.id,album.like,album.star,album.imgs,album.content,album.userid,`user`.`name`,user.icon,classify.name as classname from album left JOIN `user` ON album.userid = `user`.id left join classify on album.classid=classify.id where user.name like \'%'.$text.'%\' or album.content like \'%'.$text.'%\'  order by date desc');
        return $list;
    }
    public function getAlbumByUserId(Request $request){
        $param = $request->param();
        $id = $param['id'];
        $list = Db::query('select album.id,album.like,album.star,album.imgs,album.content,album.userid,`user`.`name`,user.icon,classify.name as classname from album left JOIN `user` ON album.userid = `user`.id left join classify on album.classid=classify.id where album.userid = \''.$id.'\'  order by date desc');
        return $list;
    }
    public function getAlbumList(Request $request){
        $param = $request->param();
        $index = (int)$param['index'];
        $page = (int)$param['page'];
        $list = Db::query('select album.id,album.like,album.star,album.imgs,album.content,album.userid,`user`.`name`,user.icon,classify.name as classname from album left JOIN `user` ON album.userid = `user`.id left join classify on album.classid=classify.id order by date desc limit '.($index)*$page.','.$page);
        return $list;
    }
    public function Del(Request $request){
        $param = $request->post(false);
        $id = $param['id'];
        $resp = Db::execute('DELETE FROM album WHERE album.id = ? and album.userid = ?',[$id,Session::get('userid')['id']]);
        if($resp>0){
            return json('',200);
        }else{
            return json('',500);
        }
    }

    public function upload(Request $request){
        // 获取表单上传文件
        $files = $request->file('file');
        $content = $request->only(['content']);
        $classid = $request->only(['classid']);
        $imgs = array();
        foreach($files as $file){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                array_push($imgs,$info->getSaveName());
            }else{
                return json(null,500);
            }    
        }
        $id=md5(time() . mt_rand(1,1000000));
        $a = Db::execute('insert into album (id, userid,content,imgs,classid,date) values (?, ?,?,?,?,now())',[$id,Session::get('userid')['id'],$content['content'],implode(',',$imgs),$classid['classid']]);
    }
}
